home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / DemoGroup.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  10.9 KB  |  313 lines

  1. /*
  2.  * @(#)DemoGroup.java    1.22 98/09/21
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import javax.swing.*;
  19. import javax.swing.border.*;
  20. import javax.swing.event.ChangeEvent;
  21. import javax.swing.event.ChangeListener;
  22. import java.util.Vector;
  23. import java.net.URL;
  24. import java.io.File;
  25.  
  26.  
  27. /**
  28.  * DemoGroup handles multiple demos inside of a panel.  Demos are loaded
  29.  * dynamically this is achieved by a directory listing of all the class 
  30.  * files in a demo group directory.
  31.  * Demo groups can be loaded individually, for example : 
  32.  *      java DemoGroup Fonts
  33.  * Loads all the demos found in the demos/Fonts directory.
  34.  */
  35. public class DemoGroup extends JPanel implements MouseListener, ChangeListener, ActionListener {
  36.  
  37.     private static Font font = new Font("serif", Font.PLAIN, 10);
  38.     private static EmptyBorder emptyB = new EmptyBorder(5,5,5,5);
  39.     private static BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED);
  40.     private String groupName;
  41.     public JPanel clonePanels[];
  42.     public JTabbedPane tabbedPane;
  43.  
  44.  
  45.     public DemoGroup(String name) {
  46.  
  47.         groupName = name;
  48.  
  49.         setLayout(new BorderLayout());
  50.  
  51.         JPanel p = new JPanel(new GridLayout(0,2));
  52.         p.setBorder(new CompoundBorder(emptyB, bevelB));
  53.  
  54.         // Retrieve class names in a specified demos/* directory
  55.         URL url = DemoGroup.class.getResource("demos/" + name);
  56.         File dir = new File(url.getFile());
  57.         if (dir != null && dir.isDirectory()) {
  58.             String list[] = dir.list();
  59.             Java2Demo.sort(list);
  60.             Vector vector = new Vector();
  61.             for (int i = 0; i < list.length; i++) {
  62.                 if (list[i].indexOf('$') == -1 && list[i].endsWith(".class")) {
  63.                     String s1 = list[i].substring(0,list[i].indexOf('.'));
  64.                     vector.add("demos." + name + "." + s1);
  65.                 }
  66.             }
  67.             if (vector.size()%2 == 1) {
  68.                 p.setLayout(new GridBagLayout());
  69.             } 
  70.             for (int i = 0; i < vector.size(); i++) {
  71.                 DemoPanel dp = new DemoPanel((String) vector.elementAt(i));
  72.                 dp.setDemoBorder(p);
  73.                 if (dp.surface != null) {
  74.                     dp.surface.addMouseListener(this);
  75.                     dp.surface.setMonitor(Java2Demo.performancemonitor != null);
  76.                 } 
  77.                 if (p.getLayout() instanceof GridBagLayout) {
  78.                     int x = p.getComponentCount() % 2;
  79.                     int y = p.getComponentCount() / 2;
  80.                     int w = i == vector.size()-1 ? 2 : 1;
  81.                     Java2Demo.addToGridBag(p,dp,x,y,w,1,1,1);
  82.                 } else {
  83.                     p.add(dp);
  84.                 }
  85.             }
  86.         } else {
  87.             System.out.println("Couldn't find " + name);
  88.         }
  89.  
  90.         add(p);
  91.     }
  92.  
  93.  
  94.     public void mouseClicked(MouseEvent e) {
  95.  
  96.         if (tabbedPane == null) {
  97.             shutDown(getPanel());
  98.             JPanel p = new JPanel(new BorderLayout());
  99.             p.setBorder(new CompoundBorder(emptyB, bevelB));
  100.  
  101.             tabbedPane = new JTabbedPane();
  102.             tabbedPane.setFont(font);
  103.  
  104.             JPanel tmpP = (JPanel) getComponent(0);
  105.             tabbedPane.addTab(groupName, tmpP);
  106.  
  107.             clonePanels = new JPanel[tmpP.getComponentCount()];
  108.             for (int i = 0; i < clonePanels.length; i++) {
  109.                 clonePanels[i] = new JPanel(new BorderLayout());
  110.                 DemoPanel dp = (DemoPanel) tmpP.getComponent(i);
  111.                 DemoPanel c = new DemoPanel(dp.className);
  112.                 c.setDemoBorder(clonePanels[i]);
  113.                 if (c.surface != null) {
  114.                     c.surface.setMonitor(Java2Demo.performancemonitor != null);
  115.                     c.toolbar.cloneB = 
  116.                         c.toolbar.addTool("clone.gif","Clone the Surface",this);
  117.                 } 
  118.                 clonePanels[i].add(c);
  119.                 String s = dp.className.substring(dp.className.indexOf('.')+1);
  120.                 tabbedPane.addTab(s, clonePanels[i]);
  121.             }
  122.             p.add(tabbedPane);
  123.             remove(tmpP);
  124.             add(p);
  125.  
  126.             tabbedPane.addChangeListener(this);
  127.             validate();
  128.         }
  129.  
  130.         String className = e.getComponent().toString();
  131.         className = className.substring(0, className.indexOf('['));
  132.  
  133.         if (Java2Demo.controls != null) {
  134.             // even if Controls.toolBarCB is off, lets make visible.
  135.             if (!Java2Demo.controls.toolBarCB.isSelected()) {
  136.                 Java2Demo.controls.toolBarCB.setSelected(true);
  137.             }
  138.         }
  139.  
  140.         for (int i = 0; i < tabbedPane.getTabCount(); i++) {
  141.             String s1 = className.substring(className.indexOf('.')+1);
  142.             if (tabbedPane.getTitleAt(i).equals(s1)) {
  143.                 tabbedPane.setSelectedIndex(i);
  144.                 break;
  145.             }
  146.         }
  147.  
  148.         validate();
  149.     }
  150.  
  151.     public void mousePressed(MouseEvent e) { }
  152.     public void mouseReleased(MouseEvent e) { }
  153.     public void mouseEntered(MouseEvent e) { }
  154.     public void mouseExited(MouseEvent e) { }
  155.  
  156.  
  157.     public void actionPerformed(ActionEvent e) {
  158.         JButton b = (JButton) e.getSource();
  159.         if (b.getToolTipText().startsWith("Clone")) {
  160.             cloneDemo();
  161.         } else {
  162.             removeClone(b.getParent().getParent().getParent());
  163.         }
  164.     }
  165.  
  166.  
  167.     private int index;
  168.  
  169.     public void stateChanged(ChangeEvent e) {
  170.         shutDown((JPanel) tabbedPane.getComponentAt(index));
  171.         index = tabbedPane.getSelectedIndex();
  172.         setup(false);
  173.     }
  174.  
  175.  
  176.     public JPanel getPanel() {
  177.         if (tabbedPane != null) {
  178.             return (JPanel) tabbedPane.getSelectedComponent();
  179.         } else {
  180.             return (JPanel) getComponent(0);
  181.         }
  182.     }
  183.  
  184.  
  185.     public void setup(boolean issueRepaint) {
  186.  
  187.         JPanel p = getPanel();
  188.  
  189.         // Let PerformanceMonitor know which demos are running
  190.         if (Java2Demo.performancemonitor != null) {
  191.             Java2Demo.performancemonitor.mc.setPanel(p);
  192.             Java2Demo.performancemonitor.mc.setSurfaceState();
  193.         }
  194.  
  195.         // .. toolbar check against global controls settings ..
  196.         // .. & start demo if need be ..
  197.         for (int i = 0; i < p.getComponentCount(); i++) {
  198.             DemoPanel dp = (DemoPanel) p.getComponent(i);
  199.             // Check toolbar against global controls
  200.             if (dp.surface == null || dp.toolbar == null) {
  201.                 continue;
  202.             }
  203.             ToolBar tb = dp.toolbar;
  204.             if (tb.startStopB != null && tb.startStopB.isSelected()) {
  205.                dp.surface.start();
  206.             }
  207.             Controls c = Java2Demo.controls;
  208.             if (c == null) {
  209.                 continue;
  210.             }
  211.             tb.verboseFlag = c.verboseCB.isSelected();
  212.             tb.issueRepaint = issueRepaint;
  213.             JButton b[] = {tb.aliasB, tb.renderB, tb.textureB, tb.compositeB};
  214.             JCheckBox cb[] = {c.aliasCB, c.renderCB, c.textureCB,c.compositeCB};
  215.             for (int j = 0; j < b.length; j++) {
  216.                 if (b[j].isSelected() != cb[j].isSelected()) {
  217.                     b[j].doClick();
  218.                 }
  219.             }
  220.             if ((tb.isVisible() && !c.toolBarCB.isSelected()) ||
  221.                 (!tb.isVisible() && c.toolBarCB.isSelected())) {
  222.                 tb.setVisible(c.toolBarCB.isSelected());
  223.                 dp.validate();
  224.             }
  225.  
  226.             if (c.imgTypeCombo.getSelectedIndex() != tb.imgTypeCombo.getSelectedIndex()) {
  227.                 tb.imgTypeCombo.setSelectedIndex(c.imgTypeCombo.getSelectedIndex());
  228.             }
  229.             tb.issueRepaint = true;
  230.         } 
  231.     }
  232.  
  233.  
  234.     public void shutDown(JPanel p) {
  235.         for (int i = 0; i < p.getComponentCount(); i++) {
  236.             DemoPanel dp = (DemoPanel) p.getComponent(i);
  237.             if (dp.surface != null) {
  238.                 dp.surface.stop();
  239.                 dp.surface.bimg = null;
  240.             }
  241.         } 
  242.         System.gc();
  243.     }
  244.  
  245.  
  246.     public void cloneDemo() {
  247.         int i = tabbedPane.getSelectedIndex() - 1;
  248.         if (clonePanels[i].getComponentCount() == 1) {
  249.             clonePanels[i].invalidate();
  250.             clonePanels[i].setLayout(new GridLayout(0,2,2,2));
  251.             clonePanels[i].validate();
  252.         }
  253.         DemoPanel original = (DemoPanel) getPanel().getComponent(0);
  254.         DemoPanel clone = new DemoPanel(original.className);
  255.         clone.setDemoBorder(clonePanels[i]);
  256.         clone.toolbar.cloneB = 
  257.               clone.toolbar.addTool("remove.gif","Remove the Surface",this);
  258.         clone.surface.start();
  259.         clone.surface.setMonitor(Java2Demo.performancemonitor != null);
  260.         clonePanels[i].add(clone);
  261.         clonePanels[i].repaint();
  262.         clonePanels[i].validate();
  263.     }
  264.  
  265.  
  266.     public void removeClone(Component theClone) {
  267.         int i = tabbedPane.getSelectedIndex() - 1;
  268.         if (clonePanels[i].getComponentCount() == 2) {
  269.             Component cmp = clonePanels[i].getComponent(0);
  270.             clonePanels[i].removeAll();
  271.             clonePanels[i].setLayout(new BorderLayout());
  272.             clonePanels[i].validate();
  273.             clonePanels[i].add(cmp);
  274.         } else {
  275.             clonePanels[i].remove(theClone);
  276.             int cmpCount = clonePanels[i].getComponentCount();
  277.             for (int j = 1;j < cmpCount; j++) {
  278.                 int top = (j+1 >= 3) ? 0 : 5;
  279.                 int left = ((j+1) % 2) == 0 ? 0 : 5;
  280.                 EmptyBorder eb = new EmptyBorder(top,left,5,5);
  281.                 SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);
  282.                 JPanel p = (JPanel) clonePanels[i].getComponent(j);
  283.                 p.setBorder(new CompoundBorder(eb, sbb));
  284.             }
  285.         }
  286.         clonePanels[i].repaint();
  287.         clonePanels[i].validate();
  288.     }
  289.  
  290.  
  291.     public static void main(String args[]) {
  292.         final DemoGroup group = new DemoGroup(args[0]);
  293.         JFrame f = new JFrame("Java2D Demo - DemoGroup");
  294.         f.addWindowListener(new WindowAdapter() {
  295.             public void windowClosing(WindowEvent e) {System.exit(0);}
  296.             public void windowDeiconified(WindowEvent e) { group.setup(false); }
  297.             public void windowIconified(WindowEvent e) { 
  298.                 group.shutDown(group.getPanel()); 
  299.             }
  300.         });
  301.         f.getContentPane().add("Center", group);
  302.         f.pack();
  303.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  304.         int WIDTH = 640;
  305.         int HEIGHT = 580;
  306.         f.setLocation(screenSize.width/2 - WIDTH/2,
  307.                           screenSize.height/2 - HEIGHT/2);
  308.         f.setSize(WIDTH, HEIGHT);
  309.         f.show();
  310.         group.setup(false);
  311.     }
  312. }
  313.